package cn.xk.ymcc.service.impl;

import cn.xk.ymcc.SearchFeignClient;
import cn.xk.ymcc.domain.*;
import cn.xk.ymcc.domain.dto.EsDto;
import cn.xk.ymcc.dto.CourseDto;
import cn.xk.ymcc.mapper.*;
import cn.xk.ymcc.result.JSONResult;
import cn.xk.ymcc.service.ICourseService;
import cn.xk.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 64009120@qq.com
 * @since 2023-03-11
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseSummaryMapper courseSummaryMapper;
    @Autowired
    private CourseResourceMapper courseResourceMapper;
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private SearchFeignClient searchFeignClient;
    @Override
    public void Gosubmitcourse(CourseDto courseDto) {

        //1.添加course数据
        Course course = courseDto.getCourse();
        //查询当前课程对应的老师
        /*List<Teacher> teachers = teacherMapper.selectBatchIds(courseDto.getTeacharIds());*/
        List<Teacher> teachers = teacherMapper.selectBatchIds(courseDto.getTeacharIds());
        String names = teachers.stream().map(Teacher::getName).collect(Collectors.joining(","));
        course.setTeacherNames(names);
        course.setStatus(4);
        course.setLoginId(45L);
        course.setLoginUserName("泥萌死了");
        super.insert(course);
        //2.添加detail数据
        CourseDetail courseDetail = courseDto.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
        //3.添加market数据
        CourseMarket courseMarket = courseDto.getCourseMarket();
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);
        //4.添加resource数据
        CourseResource courseResource = courseDto.getCourseResource();
        courseResource.setCourseId(course.getId());
        courseResourceMapper.insert(courseResource);
        //5.添加summery数据
        CourseSummary courseSummary = new CourseSummary();
        courseSummary.setId(course.getId());
        courseSummaryMapper.insert(courseSummary);
        //6.维护课程与讲师的中间表[执行效率更高，动态sql，foreach批量插入]
        courseDto.getTeacharIds().forEach(tid -> {
            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setCourseId(course.getId());
            courseTeacher.setTeacherId(tid);
            courseTeacherMapper.insert(courseTeacher);
        });
    }

    @Override
    public void onLineCourse(Long id) {

        //根据课程id获取对应market数据
        Course course = super.selectById(id);
        AssertUtil.isEquals(course.getStatus(),0,"课程已经上线");
        //根据课程id获取对应summary数据
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        CourseSummary courseSummary = courseSummaryMapper.selectById(id);
        //4.组装数据 存放到es里面
        EsDto esDto = new EsDto();
        BeanUtils.copyProperties(course,esDto);
        BeanUtils.copyProperties(courseMarket,esDto);
        BeanUtils.copyProperties(courseSummary,esDto);
        /*System.out.println(esDto);*/
        //5.发起远程调用，保存到es里面
        JSONResult jsonResult = searchFeignClient.save(esDto);
        AssertUtil.isTrue(jsonResult.isSuccess(),"es保存失败");
        //更新课程的状态
        course.setOnlineTime(new Date());//设置课程的上线日期
        course.setStatus(1);//设置状态为上线
        super.updateById(course);//更新课程的状态
        // 5.组装站内信，并推送

    }
}

